package com.myhome.money.sentinel.rule;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 许路路
 * @Date: 2019/8/9
 */
@Component
public class MyFlowRule implements InitializingBean {

    private void initFlowQpsRule() {
        FlowRuleManager.loadRules(getRuleList());
    }

    private List<FlowRule> getRuleList() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("resource001");
        // set limit qps to 20
        rule.setCount(5);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rules.add(rule);

        FlowRule rule2 = new FlowRule("resource002");
        // set limit qps to 20
        rule2.setCount(10);
        rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule2.setLimitApp("default");
        rules.add(rule2);
        return rules;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initFlowQpsRule();
    }
}
